<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * FileUpload: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */
namespace app\lnadmin\controller;
use app\core\InitController;
use think\facade\View;
use think\facade\Db;

class FileUpload extends InitController
{
    /**
     * 管理员首页JSON
     * @param string $limit 分页
     * @throws \think\db\exception\DbException
     */
    public function index_json($limit='15')
    {
        $list  = \app\lnadmin\model\admin\Image::order('id', 'desc')->where('types', Upload::TYPE_FILEUPLOAD)->field([
            'id',
            'status',
            'url',
            'file_size',
            'mine',
            'create_time',
           ])->paginate($limit);
        if (!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['urls'] = basename($v['url']);
               
            }
        }
        $this->result($list);
    }


    // 列表
    public function list()
    { 
     return view('file');
     
    }


public function add()
    {

              $param = $this->request->param();
          if(empty($param['image'])){
            return $this->error('文件不存在');
          }
            //保存图片
                $data = \app\lnadmin\model\admin\Image::create([
                    'url' => $param['image'],
                    'types' => Upload::TYPE_FILEUPLOAD,
                    'file_size' => $param['filesize'],
                    'table_id' => 0,
                    'mine' => $param['mine']
             ]);
           if ($data == true) {
        return $this->success('', '', '保存成功');
           }else{
               return $this->success('', '', '保存失败');
           }
    } 

   // 删除
    public function del()
    {
        
        if ($this->request->isPost()) { 
            $param = $this->request->param();
            $subString = $param['url'];
            $file_path = substr($subString, 1);
            if (!file_exists($file_path)) { 
            
                return $this->success('', '', '备份文件不存在'.$file_path);
            }  
            @unlink($file_path);

            $result = \app\lnadmin\model\admin\Image::destroy($param['id']);

            if ($result == true) { 
            
                return $this->success('', '', '删除成功');
            }  
           
        }
    }
}